package nu.member.service;

import java.io.File;
import java.io.IOException;
import java.util.List;

import nu.board.model.GroupVO;
import nu.board.model.MemberGroupVO;
import nu.member.model.MemberDao;
import nu.member.model.MemberVO;

import org.springframework.web.multipart.MultipartFile;

public class MemberServiceImpl implements MemberService{
	private MemberDao memberDao;
	//private String path="C:\\java-kosta\\workspace\\mediaproject\\nu\\WebContent\\uploadFile\\"; // 테스트용 workspace file path
	private String path = "C:\\java-kosta\\servers\\media-tomcat\\webapps\\nu\\uploadFile\\"; // 실제 서버 file path
	
	public MemberServiceImpl (MemberDao memberDao){
		this.memberDao=memberDao;
	}
	
	@Override
	public MemberVO login(MemberVO vo) throws Exception {
		MemberVO rvo = memberDao.selectLogin(vo);
		return rvo;
	}
	
	@Override
	public MemberVO registerMember(MemberVO vo) throws Exception {
		if(!vo.getUploadfile().isEmpty()){
			MultipartFile mFile=vo.getUploadfile();
			vo.setOrgfilename(mFile.getOriginalFilename());
			vo.setSavefilename(System.currentTimeMillis()+"_"+mFile.getOriginalFilename());
		}else{
			vo.setOrgfilename("default.png");
			vo.setSavefilename("default.png");
		}
		
		int result=memberDao.insertMember(vo);
		
		if(result!=0&&!vo.getUploadfile().isEmpty()){
			MultipartFile mFile=vo.getUploadfile();
			// 업로드할 원본 파일을 이동할 파일 객체를 생성
			File copyFile = new File(path+vo.getSavefilename());
			try {
				mFile.transferTo(copyFile);
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
		}
		return vo;
	}
	
	@Override
	public MemberVO updateMember(MemberVO vo) throws Exception {
		// 기존에 파일을 가지고 있는 애라면 원래 파일을 삭제하고 새 파일을 등록한다...
		MemberVO originVO = memberDao.selectMemberById(vo.getId());
		System.out.println(vo+" --- "+originVO);
		if(!vo.getUploadfile().isEmpty()){ 
			//기존파일이 default가 아니고 update하려는 파일이 있을경우... 
			//기존파일을 지우고 upload한파일로 처리
			MultipartFile mFile=vo.getUploadfile();
			vo.setOrgfilename(mFile.getOriginalFilename());
			vo.setSavefilename(System.currentTimeMillis()+"_"+mFile.getOriginalFilename());
		}else{
			vo.setOrgfilename(originVO.getOrgfilename());
			vo.setSavefilename(originVO.getSavefilename());
		}
		
		System.out.println("업데이트완료 : "+vo);
		int result=memberDao.updateMember(vo);
		
		if(result!=0&&!vo.getUploadfile().isEmpty()){
			System.out.println("이전파일 지우고 새파일로 변경함");
			
			File originFile = new File(path+originVO.getSavefilename());
			originFile.delete();
			
			MultipartFile mFile=vo.getUploadfile();
			// 업로드할 원본 파일을 이동할 파일 객체를 생성
			File copyFile = new File(path+vo.getSavefilename());
			try {
				mFile.transferTo(copyFile);
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
		}
		return vo;
	}

	@Override
	public int idCheck(String id) throws Exception {
		return memberDao.selectCountById(id);
	}
	@Override
	public List<GroupVO> getGroupListByMember(MemberVO mvo)throws Exception{
		List<GroupVO>list=memberDao.selectGroupListByMemberId(mvo.getId());
		return list;
	}

	@Override
	public int joinGroup(MemberGroupVO mgvo)
			throws Exception {
		int result=memberDao.insertMemberToGroup(mgvo);
		result=incrementGroupCurrent(mgvo.getGroup_id());
		return result;
	}
	@Override
	public int incrementGroupCurrent(int group_id) throws Exception{
		return memberDao.updateGroupCurrent(group_id);
	}

	/**
	 * nickname을 포인트에 따라 설정해준다.
	 */
	@Override
	public void setNickName(MemberVO vo) throws Exception {
		memberDao.updateStudyNickName(vo);
		memberDao.updateSportsNickName(vo);
		memberDao.updateSmokingNickName(vo);
		memberDao.updateFinanceNickName(vo);
		memberDao.updateLivingNickName(vo);
	}

	/**
	 * member의 모든 정보를 가져온다
	 */
	@Override
	public MemberVO getMemberInfo(MemberVO vo) throws Exception {
		return memberDao.selectMemberInfo(vo);
	}

	@Override
	public void increaseStudyPoint(String writer) throws Exception {
		memberDao.plusStudyPointById(writer);
	}

	@Override
	public void increaseSportsPoint(String writer) throws Exception {
		memberDao.plusSportsPointById(writer);
	}

	@Override
	public void increaseSmokingPoint(String writer) throws Exception {
		memberDao.plusSmokingPointById(writer);
	}

	@Override
	public void increaseFinancePoint(String writer) throws Exception {
		memberDao.plusFinancePointById(writer);
	}

	@Override
	public void increaseLivingPoint(String writer) throws Exception {
		memberDao.plusLivingPointById(writer);
	}

	@Override
	public String getNickContent(String nick, String type) throws Exception {
		return memberDao.selectNickContent(nick,type);
	}

	@Override
	public String getPhotoById(String id) throws Exception {
		return memberDao.selectSavefilenameById(id);
	}
}
